package com.yutel.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class TestClientTimeout {

	public static void main(String[] args) {
		Socket client = null;
		DataInputStream in = null;
		DataOutputStream out = null;
		long start = System.currentTimeMillis();
		try {
			client = new Socket();
			InetAddress addr=Inet4Address.getByName("127.0.0.1");
			client.connect(new InetSocketAddress(addr,52001), 4000);
			//
			out = new DataOutputStream(client.getOutputStream());
			byte[] sendmsg = "asdf".getBytes();
			out.writeInt(sendmsg.length);
			out.write(sendmsg);
			out.flush();
			//
			in = new DataInputStream(client.getInputStream());
			int msgLen = in.readInt();
			System.out.println("clien size=" + msgLen);
			byte[] data = new byte[msgLen];
			in.readFully(data);
			System.out.println("service msg=" + data);
			long end = System.currentTimeMillis();
			System.out.println("normal total=" + (end - start) / 1000);
		} catch (Exception e) {
			long end = System.currentTimeMillis();
			System.out.println("exception total=" + ((end - start) / 1000));
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
